﻿using Corina.DataContext.Service;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Master
{
    public class WeburlTopMaster : MasterBase
    {
        public ResultInfo GetFetchedTopurl(string account, string password, PlatformTypeValue platform, AccountTypeValue accountType)
        {
            ResultValue resultValue = ResultValue.Unknown; //VerifyAccount<ManagerInfo>(account, password);

            int roleOrLevel = 0;

            switch(accountType)
            {
                case AccountTypeValue.Manager:
                    resultValue = VerifyAccount<ManagerInfo>(account, password);
                    break;
                case AccountTypeValue.User:
                    resultValue = VerifyAccount<UserInfo>(account, password);
                    break;
            }

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                switch(accountType)
                {
                    case AccountTypeValue.User:
                        roleOrLevel = GetUserLevel(account).Id;
                        break;
                    case AccountTypeValue.Manager:
                        roleOrLevel = GetManagerRole(account).Id;
                        break;
                }
                //int roleId = GetManagerRole(account).Id;

                var items = GetInstance<WeburlTopService>().GetFetchedTopurl(ConnectionString(DatabaseKeys.CORINA_CORE), roleOrLevel, platform, accountType);

                result.Entities.AddRange(items);
            }

            return result;
        }

        public ResultInfo GetAllTopurl(string account, string password, string title, params PlatformTypeValue[] platforms)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {

                var items = GetInstance<WeburlTopService>().GetAllTopurl(ConnectionString(DatabaseKeys.CORINA_CORE), title, platforms);

                result.Entities.AddRange(items);
            }

            return result;
        }

        public ResultInfo Update(string account, string password, WeburlTopInfo entity, EntityOperation operation)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<WeburlTopService>().Update(ConnectionString(DatabaseKeys.CORINA_CORE), entity, operation);
                if(!flag)
                {
                    result.Result = ResultValue.Failed;
                }
            }

            return result;
        }

        public ResultInfo GetPermission(string account, string password, int roleOrLevelId, AccountTypeValue accountType)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                var items = GetInstance<TopurlPermissionService>().GetAccessUrl(ConnectionString(DatabaseKeys.CORINA_CORE), roleOrLevelId, accountType);
                result.Entities.AddRange(items);
            }

            return result;
        }

        public ResultInfo UpdatePermission(string account, string password, int roleOrLevelId, AccountTypeValue accountType, params int[] updateIds)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<TopurlPermissionService>().Update(ConnectionString(DatabaseKeys.CORINA_CORE), roleOrLevelId, accountType, updateIds);
                if (!flag)
                {
                    result.Result = ResultValue.Failed;
                }
            }

            return result;
        }
    }
}
